package com.ycp.erp.bill.ui.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.jakewharton.rxbinding2.view.RxView;
import com.one.common.config.RouterPath;
import com.one.common.utils.KeyboardUtil;
import com.one.common.view.adapter.PagerFragmentAdapter;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.base.BaseFragment;
import com.one.common.view.slidingtab.ScrollViewPagerAdapter;
import com.one.common.view.slidingtab.SlidingTabLayout;
import com.one.common.view.widget.ClearEditView;
import com.one.common.view.widget.MyTitleBar;
import com.ycp.erp.R;
import com.ycp.erp.bill.ui.fragment.BillFragment;
import com.ycp.erp.view.HintDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

/**
 * Class Des:账单管理
 * Created by bjh on 2021/9/7.
 */
@Route(path = RouterPath.BILL_MANAGER)
public class BillManagerActivity extends BaseActivity {
    @BindView(R.id.ce)
    ClearEditView ce;
    @BindView(R.id.slide)
    SlidingTabLayout mSlidTab;
    @BindView(R.id.tvBeginDate)
    TextView tvBeginDate;
    @BindView(R.id.ivBeginDateTriangle)
    ImageView ivBeginDateTriangle;
    @BindView(R.id.rlBeginDate)
    RelativeLayout rlBeginDate;
    @BindView(R.id.tvEndDate)
    TextView tvEndDate;
    @BindView(R.id.ivEndDateTriangle)
    ImageView ivEndDateTriangle;
    @BindView(R.id.rlEndDate)
    RelativeLayout rlEndDate;
    @BindView(R.id.vp_order)
    ViewPager vpOrder;
    @BindView(R.id.tvSubmit)
    TextView tvSubmit;
    @BindView(R.id.llBottom)
    LinearLayout llBottom;
    private String[] tabs = {"全部账单", "应付账单", "应收账单", "已清账单"};
    private PagerFragmentAdapter pagerAdapter;

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("账单管理").hideLine();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_history;
    }

    @SuppressLint("CheckResult")
    @Override
    public void initView() {
        super.initView();
         tvSubmit.setText("确认清账");
        llBottom.setVisibility(View.VISIBLE);
        RxView.clicks(rlBeginDate).throttleFirst(1, TimeUnit.SECONDS).subscribe(o -> {
            new KeyboardUtil(mContext).hideSoftInputFromWindow(myRxActivity);
            TimePickerView pvTime = new TimePickerBuilder(myRxActivity, new OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {
                    tvBeginDate.setText("开始日期：" + new SimpleDateFormat("yyyy-MM-dd").format(date));
                }
            })
                    .setSubmitColor(getResources().getColor(R.color.root_green))
                    .setCancelColor(getResources().getColor(R.color.gray_d6))
                    .setLineSpacingMultiplier(2.8f)
                    .build();
            pvTime.show();
        });
        RxView.clicks(rlEndDate).throttleFirst(1, TimeUnit.SECONDS).subscribe(o -> {
            new KeyboardUtil(mContext).hideSoftInputFromWindow(myRxActivity);
            TimePickerView pvTime = new TimePickerBuilder(myRxActivity, new OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {
                    tvEndDate.setText("结束日期：" + new SimpleDateFormat("yyyy-MM-dd").format(date));
                }
            })
                    .setSubmitColor(getResources().getColor(R.color.root_green))
                    .setCancelColor(getResources().getColor(R.color.gray_d6))
                    .setLineSpacingMultiplier(2.8f)
                    .build();
            pvTime.show();
        });
        RxView.clicks(tvSubmit).throttleFirst(1, TimeUnit.SECONDS).subscribe(o -> {
            new KeyboardUtil(mContext).hideSoftInputFromWindow(myRxActivity);
            HintDialog dialog = HintDialog.getInstance();
            dialog.setInputListener(new HintDialog.InputListener() {
                @Override
                public void onCancel() {

                }

                @Override
                public void onConfirm(String data) {
                }
            });
            dialog.setHintMsg("提示","确认将所选的账单清账?");
            dialog.show(getSupportFragmentManager(),"");
        });
        initPageTitle();
//        DefaultExtra defaultExtra= (DefaultExtra) getIntent().getSerializableExtra(DefaultExtra.getExtraName());
//        vpOrder.setCurrentItem(defaultExtra.getNum());
    }

    /**
     * 初始化头部
     */
    private void initPageTitle() {
        ArrayList<BaseFragment> taskFragments = getTaskFragments();
        pagerAdapter = new PagerFragmentAdapter(getSupportFragmentManager(), taskFragments);
        vpOrder.setAdapter(pagerAdapter);
        vpOrder.setOffscreenPageLimit(4);
        ScrollViewPagerAdapter adapter = new ScrollViewPagerAdapter(getSupportFragmentManager(), taskFragments, tabs);
        vpOrder.setOffscreenPageLimit(taskFragments.size());
        vpOrder.setAdapter(adapter);
        mSlidTab.setViewPager(vpOrder);

    }

    /**
     * 各种状态的任务列表
     */
    private ArrayList<BaseFragment> getTaskFragments() {
        ArrayList<BaseFragment> fragments = new ArrayList<>();
        fragments.add(BillFragment.getInstance(1));
        fragments.add(BillFragment.getInstance(1));
        fragments.add(BillFragment.getInstance(1));
        fragments.add(BillFragment.getInstance(2));
        return fragments;
    }
}